package com.wanmait.translate.system.service.impl;

import com.wanmait.translate.dto.ListToDo;
import com.wanmait.translate.dto.TypeContent;
import com.wanmait.translate.init.service.impl.InitAuditSericeImpl;
import com.wanmait.translate.pojo.SupplierTravel;
import com.wanmait.translate.supplier.mapper.SupplierTravelMapper;
import com.wanmait.translate.system.controller.IndexController;
import com.wanmait.translate.system.mapper.SystemAuditedMapper;
import com.wanmait.translate.system.service.AuditedService;
import com.wanmait.translate.utils.AuditEnum;
import com.wanmait.translate.utils.DateUtil;
import com.wanmait.translate.utils.RedisCacheMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @Author huweibin
 * @Description 所有审核的业务逻辑
 * @Date 11:55 2020/11/23
 **/
@Service
public class AuditedServiceImpl implements AuditedService {
    private static final Logger LOG= LoggerFactory.getLogger(IndexController.class);
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SystemAuditedMapper systemAuditedMapper;
    @Resource
    private InitAuditSericeImpl initAuditSerice;
    @Resource
    private SupplierTravelMapper supplierTravelMapper;
    /*
     * @Author huweibin
     * @Description 获得最近的审核
     * @Date 14:20 2020/11/23
     **/
    public ListToDo listToDo(List<AuditEnum> prems,String date)  {
        ListToDo res = new ListToDo();
        try {
            //获得此月所有天
            List<String> days = DateUtil.getMonthMoreDay(date);
            //循环,获得审核情况
            days.forEach((day)-> {
                 this.countOneDayToDo(res,prems,day);
            });
        } catch (ParseException e) {
            e.printStackTrace();
        }
        LOG.info("查询结果为"+res);
        return res;
    }

    /*
     * @Author huweibin
     * @Description  指定日期的所有状态数量
     * @Date 14:19 2020/11/23
     **/
    public void countOneDayToDo(ListToDo listToDo,List<AuditEnum> prems,String day) {
        //获得cahce
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String auditKey =null;
        Integer count =null;
        //所有审核类型
        for (AuditEnum prem : prems) {
            TypeContent typeContent = new TypeContent(prem);
            //所有可能状态
            for (Map.Entry state : prem.getStates().entrySet()) {
                //获得缓存key
                 auditKey = RedisCacheMessage.getAuditKey(prem, (Integer) state.getKey(), day);
                Object element = valueOperations.get(auditKey);
                if (element!=null){
                    //获得当前状态数量
                    count = (Integer)element;
                    if (count!=0){
                        //保存
                        typeContent.addState((String)state.getValue(),(Integer)state.getKey(),count);
                    }
                }else{
                    //如果为空说明之前没有缓存过
                    try {
                        initAuditSerice.initOneDayToDo(day);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
            //如果不是一个状态没有
            if (typeContent.getStates().size()!=0){
                listToDo.addTypeContent(day,typeContent);
            }
        }
    }

    /**
     * @Author huweibin
     * @Description 获得指定日期的审核
     * @Date 19:59 2020/11/24
     **/
    @Override
    public ListToDo listOneDayToDo(List<AuditEnum> prems, String date) {
        //结果
        ListToDo res = new ListToDo();
        for (AuditEnum prem : prems) {
            switch (prem) {
                //如果是出差审核
                case TRAVELED_AUDIT: {
                    TypeContent typeContent = new TypeContent(prem);
                    //保存今天所有出差审核情况
                    typeContent.addList(systemAuditedMapper.oneDayToDoList(date));
                    //保存到结果
                    res.addTypeContent(date,typeContent);
                    break;
                }
            }

        }
        return res;
    }

    @Override
    public SupplierTravel getIravleAuditInfo(Integer id) {
        return supplierTravelMapper.getSupplierTravelById(id);
    }

    /**
     * @Author huweibin
     * @Description 对这个方法加了aop
     * @Date 19:34 2020/11/28
     **/
    @Override
    public void updateAudit(String id, Integer stateId,String day,AuditEnum value) {
        supplierTravelMapper.updateAudit( id,  stateId);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //如果为通过
        if (stateId==1){
            String add = RedisCacheMessage.getAuditKey(value, 1, day);
            String del = RedisCacheMessage.getAuditKey(value, 0, day);

            Long increment = valueOperations.increment(add, 1L);
            valueOperations.decrement(del, 1L);
            LOG.info("增加的key为"+add);
            LOG.info("增加的key为"+del);
        }else if (stateId==2){
            String add = RedisCacheMessage.getAuditKey(value, 2, day);
            String del = RedisCacheMessage.getAuditKey(value, 0, day);
            valueOperations.increment(add, 1L);
            valueOperations.decrement(del, 1L);
            LOG.info("增加的key为"+add);
            LOG.info("增加的key为"+del);
        }


    }
}
